package com.lgkj.ui.manager;

import android.content.Intent;
import android.view.View;
import android.widget.ListView;

import com.google.gson.Gson;
import com.lgkj.R;
import com.lgkj.adapter.ShopAmbientAdapter;
import com.lgkj.adapter.ShopTrendsAdapter;
import com.lgkj.bean.ShopTrendsBean;
import com.lgkj.ui.BaseActivity;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/6/25 0025.
 */

public class ShopTrendsActivity extends BaseActivity {
    @BindView(R.id.lv_item)
    ListView lvItem;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    private ShopTrendsAdapter adapter;
    private ShopTrendsBean bean;
    int page = 1;
    public static boolean isRefresh = true;

    @Override
    public int getLayoutId() {
        return R.layout.activity_shop_trends;
    }

    @Override
    public void initView() {
        setTitle("店铺动态");
        adapter = new ShopTrendsAdapter(ShopTrendsActivity.this);
        lvItem.setAdapter(adapter);
    }

    /**
     * http://mda.lianguangtech.com/api/Shop_Dynamic/shopDynamicListByShop?access_token=80a0d224500097b389590ce3d2c2ce17&sid=35d74b9faa1550c010484802469748b1&shop_id=287&shop_table=banbershop
     */
    @Override
    public void initData() {
        adapter.setClick(new ShopAmbientAdapter.MyClickListener() {
            @Override
            public void clickListener(View v) {
                if (v.getId() == R.id.iv_del) {
                    int id = Integer.parseInt(v.getTag().toString());
                    del(adapter.getItem(id).getId());
                }
            }
        });
        srlRefresh.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                initDatas();
            }
        });
        srlRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                initDatas();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isRefresh)
            initDatas();
    }

    private void initDatas() {
        showDiaLog("正在获取");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(ShopTrendsActivity.this));
        map.put("shop_id", CommonUtils.getShopId(ShopTrendsActivity.this));
        map.put("shop_table", CommonUtils.getShopTable(ShopTrendsActivity.this));
        map.put("page", page + "");
        HttpUtils.postHttp(map, CommonConstant.shopDynamiclist, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (page == 1) {
                    adapter.dataClean();
                }
                if (jsonObject.optInt("code") == 0) {
                    finishLoadingData(true);
                    Gson mGson = new Gson();
                    bean = mGson.fromJson(String.valueOf(jsonObject), ShopTrendsBean.class);
                    if (bean != null && bean.getData() != null && bean.getData().getData() != null) {
                        adapter.setData(bean.getData().getData());
                    }
                } else {
                    finishLoadingData(false);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
                if (page != 1) {
                    page--;
                    srlRefresh.finishLoadmore(false);
                } else {
                    srlRefresh.finishRefresh(false);
                }
            }
        });
    }

    /**
     * 结束刷新数据
     */
    private void finishLoadingData(boolean succ) {
        if (page == 1) {
            srlRefresh.finishRefresh(succ);
        } else {
            srlRefresh.finishLoadmore(succ);
        }
    }

    @OnClick(R.id.tv_add)
    public void onClick() {
        startsActivity(new Intent(ShopTrendsActivity.this, ShopTrendsAddActivity.class));
        isRefresh = false;
    }

    private void del(String id) {
        showDiaLog("正在删除");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(ShopTrendsActivity.this));
        map.put("dynamic_id", id);
        HttpUtils.postHttp(map, CommonConstant.shopDynamicdel, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                toastLong(jsonObject.optString("msg"), 0);
                if (jsonObject.optInt("code") == 0) {
                    page = 1;
                    initDatas();
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }
}
